﻿//文本框提示
Function.registerNameSpace("Page");

Page.InputParmpt=function(obj,count){
    Function.registerClass(this);
    if(typeof(obj)=="string") obj=$$(obj);
    this._oldValue="";

    this._itemMouseover=EventOperate.createDelegate(this,this.__itemMouseover);
	this._inputKeydown=EventOperate.createDelegate(this,this.__inputKeydown);
	this._itemClick=EventOperate.createDelegate(this,this.__itemClick);
	this._itemMouseDown=EventOperate.createDelegate(this,this.__itemMouseDown);
	this._documentMouseDown=EventOperate.createDelegate(this,this.__documentMouseDown);
	this._inputMouseDown=EventOperate.createDelegate(this,this.__inputMouseDown);
	this._inputFocus=EventOperate.createDelegate(this,this.__inputFocus);

    this.inputBox=obj;
	this.inputBox.autocomplete="off";
    if(count) this.parmptCount=count;
    else this.parmptCount=15;
    var div=document.addElement(document.body,"div");
    div.style.display="none";
    this.parmptBox=div;
   
    EventOperate.FixedPart(div);
    this.parmptBox.className="AGuang___Page___InputPrompt";
    this.parmptBox.style.position="absolute";
    this.parmptBox.style.zIndex=99999;
    this._autoSide=new Page.Action(this.parmptBox);

	EventOperate.addEvent(obj,"keyup",this._inputKeydown);
	EventOperate.addEvent(obj,"mousedown",this._inputMouseDown);
    EventOperate.addEvent(document,"mousedown",this._documentMouseDown);
    EventOperate.addEvent(obj,"focus",this._inputFocus);
}

//设置提示框的宽度，为空表示和文本框一样宽
Page.InputParmpt.prototype.BindWidth=function(width){
    if(width) this.parmptBox.style.width=(width-2)+"px";
    else this.parmptBox.style.width=(this.inputBox.offsetWidth-2)+"px";
    this.parmptBox.style.overflow="hidden";
}

//清空文本框的输入
Page.InputParmpt.prototype.ClearInput=function(){
    this._oldValue='';
    this.inputBox.value='';
}

//设置激活提示内容的处理函数
Page.InputParmpt.prototype.Add_StartFun=function(func){
    EventOperate.addHandler(this,"StartFun",func);
}

//设置文本框被激活时的处理函数
Page.InputParmpt.prototype.Add_InputFocus=function(func){
    EventOperate.addHandler(this,"Focus",func);
}

//填充提示内容
Page.InputParmpt.prototype.Fill = function (list) {
    if (list.length < 1) {//无值
        this.parmptBox.style.display = "none";
        return;
    }


    //重新生成列表
    if (this._table) this.parmptBox.removeChild(this._table);
    this._activeIndex = -1;
    this._table = document.createElement("table");
    this._table.width = "100%";
    this._table.height = 10;
    this._table.cellPadding = 3;
    this._table.cellSpacing = 0;
    this._table.border = 0;
    this.parmptBox.appendChild(this._table);

    for (var i = 0; i < this.parmptCount; i++) {
        if (i >= list.length) break;
        var tr = this._table.insertRow(i);
        tr._index = i; tr.style.height = "2px";
        var item = list[i];
        var j = -1;
        tr.onmouseover = this._itemMouseover;
        tr.onclick = this._itemClick;
        tr.onmousedown = this._itemMouseDown;
        if (typeof (item) == "object") {
            for (var v in item) {
                j++;
                var td = tr.insertCell(j);
                td.innerHTML = item[v];
                if (!tr._v) tr._v = item[v];
                td.className = "td_" + j;
            }
        } else {
            var td = tr.insertCell(0);
            td.innerHTML = item;
            td.style.lineHeight = "100%";
            td.style.textAlign = "left";
            tr._v = item;
            td.className = "td_0";
        }
    }

    //定位下拉框
    this.parmptBox.style.display = "";
    this._autoSide.AutoSide(this.inputBox);
    document.layerGoodInIE6(this.parmptBox);
}

Page.InputParmpt.prototype.SetCssClass=function(className){
    this.parmptBox.className=className;
}

Page.InputParmpt.prototype.__changeSelected=function(index){
    if(this._table){
        var tbody=this._table.tBodies[0];
        var rows=tbody.rows;
        if(index<0||index>=rows.length) return;
        for(var i=0;i<rows.length;i++){
            document.removeClass(rows[i],"itemOver");
        }
        document.addClass(rows[index],"itemOver");
        this.inputBox.value=rows[index]._v;
        this._activeIndex=index;
    }
}

Page.InputParmpt.prototype.__itemMouseover=function(sender,e){
    this.__changeSelected(sender._index);
}

Page.InputParmpt.prototype.__itemClick=function(sender,e){
    this.__itemSelected();
}

Page.InputParmpt.prototype.__inputKeydown=function(sender,e){
    if(e.keyCode==38) this.__itemMoveUp();
    else if(e.keyCode==40) this.__itemMoveDown();
    else if(e.keyCode==13) this.__itemSelected();
    else{
        if(this._oldValue!=this.inputBox.value){
		    //this._oldValue=this.inputBox.value;
		    var _h=EventOperate.getHandler(this,"StartFun");
		    if(_h) {_h(this,this.inputBox.value);}
		}
    }
}

Page.InputParmpt.prototype.__itemMoveUp=function(){
    this.__changeSelected(this._activeIndex-1);
}

Page.InputParmpt.prototype.__itemMoveDown=function(){
    this.__changeSelected(this._activeIndex+1);
}

Page.InputParmpt.prototype.__itemSelected=function(){
    this._oldValue=this.inputBox.value;
    this.parmptBox.style.display="none";
}

Page.InputParmpt.prototype.__documentMouseDown=function(sender,e){
    this.parmptBox.style.display="none";
    //if(this._oldValue) this.inputBox.value=this._oldValue;
}
Page.InputParmpt.prototype.__inputMouseDown=function(sender,e){
    EventOperate.cancelBubble(e);
    //return false;
}
Page.InputParmpt.prototype.__itemMouseDown=function(sender,e){
    EventOperate.cancelBubble(e);
    return false;
}

Page.InputParmpt.prototype.__inputFocus=function(sender,e){
    var _h=EventOperate.getHandler(this,"Focus");
    if(_h) {_h(this,this.inputBox.value);}
}

Page.InputParmpt.prototype._clearList=function(){
    if(this._table){ 
        for(var i=this._table.rows.length-1;i>=0;i--){
            var row=this._table.rows[i];
            for(var j=row.cells.length-1;j>=0;j--){
                row.deleteCell(j);
            }
            this._table.deleteRow(i);
        }
        this._table=null;
    }
}

Page.InputParmpt.prototype.dispose=function(){
    if(this._table) this._table=null;
    this.inputBox=null;
}
Page.InputPrompt=Page.InputParmpt;